/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface.Panel;

import Controller.InputController;
import Controller.VisitController;
import DAOs.CollaboratorJpaController;
import Entity.Collaborator;
import Entity.Visit;
import javax.persistence.EntityManagerFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import siru.SIRU;

/**
 *
 * @author Nicolas
 */
public class PanelVisitCreate extends javax.swing.JPanel {

    /**
     * Creates new form PanelVisitCreate
     */
    private int VISIT_DURATION = 3;
    private Visit visit;
    private VisitController vc;
    private JFrame frame = new JFrame();
    private EntityManagerFactory emf;
    
    public PanelVisitCreate(EntityManagerFactory emf) {
        initComponents();
        this.emf = emf;
        vc = new VisitController(this.emf);
        horaInicioSpinField.setMinimum(0);
        horaInicioSpinField.setValue(8);
        horaFinSpinField.setMinimum(horaInicioSpinField.getValue());
        horaFinSpinField.setValue(20);
        horaInicioSpinField.setMaximum(horaFinSpinField.getValue());
        horaFinSpinField.setMaximum(23);
        colaboradorVisitaTextArea.setEditable(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        logoLabel2 = new javax.swing.JLabel();
        titleLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        colaboradorIDBuscarVisitaBtn = new javax.swing.JButton();
        colaboradorIDVisitaBuscarTF = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        colaboradorVisitaTextArea = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        horaInicioSpinField = new com.toedter.components.JSpinField();
        fechaDeVisita = new com.toedter.calendar.JDateChooser();
        jButton2 = new javax.swing.JButton();
        crearCitaBtn = new javax.swing.JButton();
        horaFinSpinField = new com.toedter.components.JSpinField();

        logoLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Usuario.png"))); // NOI18N

        titleLabel2.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        titleLabel2.setText("Registro de Nueva Visita ULATA");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(logoLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleLabel2)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(titleLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos de Visita"));

        colaboradorIDBuscarVisitaBtn.setText("Buscar");
        colaboradorIDBuscarVisitaBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colaboradorIDBuscarVisitaBtnActionPerformed(evt);
            }
        });

        colaboradorIDVisitaBuscarTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colaboradorIDVisitaBuscarTFActionPerformed(evt);
            }
        });

        colaboradorVisitaTextArea.setColumns(20);
        colaboradorVisitaTextArea.setRows(5);
        jScrollPane1.setViewportView(colaboradorVisitaTextArea);

        jLabel4.setText("Colaborador ID:");

        jLabel1.setText("Fecha de Visita: ");

        jLabel2.setText("Hora de Inicio: ");

        jLabel3.setText("Hora de Fin:");

        horaInicioSpinField.setMaximum(23);
        horaInicioSpinField.setMinimum(0);
        horaInicioSpinField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                horaInicioSpinFieldPropertyChange(evt);
            }
        });

        jButton2.setText("Volver");

        crearCitaBtn.setText("Crear Visita");
        crearCitaBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearCitaBtnActionPerformed(evt);
            }
        });

        horaFinSpinField.setMaximum(23);
        horaFinSpinField.setMinimum(0);
        horaFinSpinField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                horaFinSpinFieldPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(horaFinSpinField, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(horaInicioSpinField, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(fechaDeVisita, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(crearCitaBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(colaboradorIDVisitaBuscarTF, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(colaboradorIDBuscarVisitaBtn)))
                .addGap(31, 31, 31))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(colaboradorIDBuscarVisitaBtn)
                            .addComponent(colaboradorIDVisitaBuscarTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(73, 73, 73))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(fechaDeVisita, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(horaInicioSpinField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(horaFinSpinField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(crearCitaBtn)
                            .addComponent(jButton2))
                        .addContainerGap())))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void crearCitaBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearCitaBtnActionPerformed
        if(vc.createVisit(fechaDeVisita.getCalendar(), horaInicioSpinField.getValue(), horaFinSpinField.getValue(), colaboradorIDVisitaBuscarTF.getText())){
            CollaboratorJpaController cJPA = new CollaboratorJpaController(emf);
            Collaborator c = cJPA.findCollaborator(Long.parseLong(colaboradorIDVisitaBuscarTF.getText()));            
            visit = new Visit();
            visit.setFecha(fechaDeVisita.getCalendar());
            visit.setHoraInicio(horaInicioSpinField.getValue());
            visit.setHoraFin(horaFinSpinField.getValue());
            visit.setVisitor(c);
        }else{
            JOptionPane.showMessageDialog(frame, "No se puede crear la cita", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_crearCitaBtnActionPerformed

    private void colaboradorIDVisitaBuscarTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colaboradorIDVisitaBuscarTFActionPerformed
        colaboradorIDBuscarVisitaBtnActionPerformed(evt);
    }//GEN-LAST:event_colaboradorIDVisitaBuscarTFActionPerformed

    private void colaboradorIDBuscarVisitaBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colaboradorIDBuscarVisitaBtnActionPerformed
        if (inputController.isInteger(colaboradorIDVisitaBuscarTF.getText())) {
            colaboradorVisitaTextArea.setText(vc.findCollaborator(colaboradorIDVisitaBuscarTF.getText()));
        }
    }//GEN-LAST:event_colaboradorIDBuscarVisitaBtnActionPerformed

    private void horaInicioSpinFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_horaInicioSpinFieldPropertyChange
        horaFinSpinField.setMinimum(horaInicioSpinField.getValue() + VISIT_DURATION);
    }//GEN-LAST:event_horaInicioSpinFieldPropertyChange

    private void horaFinSpinFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_horaFinSpinFieldPropertyChange
        horaInicioSpinField.setMaximum(horaFinSpinField.getValue() - VISIT_DURATION);
    }//GEN-LAST:event_horaFinSpinFieldPropertyChange

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton colaboradorIDBuscarVisitaBtn;
    private javax.swing.JTextField colaboradorIDVisitaBuscarTF;
    private javax.swing.JTextArea colaboradorVisitaTextArea;
    private javax.swing.JButton crearCitaBtn;
    private com.toedter.calendar.JDateChooser fechaDeVisita;
    private com.toedter.components.JSpinField horaFinSpinField;
    private com.toedter.components.JSpinField horaInicioSpinField;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel logoLabel2;
    private javax.swing.JLabel titleLabel2;
    // End of variables declaration//GEN-END:variables
    private InputController inputController;
}
